<?php

namespace app\common\model\caijiatong;

use think\Cache;
use think\Model;

/**
 * 材价通 分类
 */
class Category extends Model
{
    protected $connection = 'database.db_second';//连接配置
    protected $table = 'fa_category';//连接配置
    public function getmarketcategory($area_id = 1263)
    {
        $str = 'jsoncategory' . $area_id;
        $category = json_decode(Cache::store('redis')->get($str), true);
        if (!$category) {
            $category = $this->where(['status' => 'normal', 'pid' => 0, 'type' => 'material', 'area_id' => '100001'])->field('id,name,image,flag')->select();
            if ($area_id) {
                $areacategory = $this->where(['status' => 'normal', 'pid' => 0, 'type' => 'material'])->where('area_id', $area_id)->field('id,name,image,flag')->select();
                $category = array_merge($areacategory, $category);
            }
            foreach ($category as $key => $value) {
                $categorylist = $this->table('fa_category')->where(['status' => 'normal', 'pid' => $value['id'], 'type' => 'material'])->field('id,name,flag')->select();
                foreach ($categorylist as $k => $v) {
                    $categorylist[$k]['categorylist'] = $this->table('fa_category')->where(['status' => 'normal', 'pid' => $v['id'], 'type' => 'material'])->field('id,name,flag,material_ids')->select();
                }
                $category[$key]['categorylist'] = $categorylist;
            }
            $jsoncategory = json_encode($category);
            Cache::store('redis')->set($str, $jsoncategory);
        }
        return $category;
    }

}
